﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using System.ComponentModel;

namespace STEditor.Classes
{

    [System.Diagnostics.DebuggerDisplay("X {X}, Y {Y}, Row {Row}, Col {Column}, Blocking {Blocking}")]
    public class STTile : NotifyObjectBase
    {

        public STTile()
        {
        }

        private int _x;
        public virtual int X
        {
            get { return GameArea.TileWidth * Column; }
        }

        private int _y;
        public virtual int Y
        {
            get { return GameArea.TileHeight * Row; }
        }

        private STFrame _image;
        public virtual STFrame Image
        {
            get { return _image; }
            set
            {
                _image = value;
                RaisePropertyChanged("Image");
            }
        }

        private Boolean? _blocking = null;
        public Boolean Blocking
        {
            get
            {
                if (_blocking.HasValue)
                {
                    return _blocking.Value;
                }
                else if (Image != null)
                {
                    return Image.Blocking;
                }
                return false;
            }
            set
            {
                _blocking = value;
                RaisePropertyChanged("Blocking");
            }
        }

        private int _row;
        public int Row
        {
            get { return _row; }
            set
            {
                _row = value;
                RaisePropertyChanged("Row");
                RaisePropertyChanged("X");
            }
        }

        private int _column;
        public int Column
        {
            get { return _column; }
            set
            {
                _column = value;
                RaisePropertyChanged("Row");
                RaisePropertyChanged("X");
            }
        }

        public virtual int Width { get { return GameArea.TileWidth; } }
        public virtual int Height { get { return GameArea.TileHeight; } }

        private STGameArea _gameArea;
        public virtual STGameArea GameArea
        {
            get { return _gameArea; }
            set
            {
                _gameArea = value;
                RaisePropertyChanged("GameArea");
            }
        }

        private STAreaTransition _transition;
        public virtual STAreaTransition Transition
        {
            get { return _transition; }
            set
            {
                _transition = value;
                RaisePropertyChanged("Transition");
            }
        }

        public ICommand ClickedCommand
        {
            get
            {
                return new ReplaceTileCommand(this);
            }
        }
        public class ReplaceTileCommand : ICommand
        {

            private STTile _tile;
            public ReplaceTileCommand(STTile tile)
            {
                _tile = tile;
                ((INotifyPropertyChanged)_tile.GameArea.Game.TileTypes).PropertyChanged += new PropertyChangedEventHandler(ReplaceTileCommand_PropertyChanged);
            }

            void ReplaceTileCommand_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                if (e.PropertyName.Equals("SelectedTile") && CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, new EventArgs());
                }
            }

            public bool CanExecute(object parameter)
            {
                if (_tile.GameArea == null)
                {
                    return false;
                }
                return _tile.GameArea.Game.TileTypes.SelectedTile != null;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                if (!_tile.GameArea.Game.FillMode)
                {
                    _tile.GameArea.SetTile(_tile.Column, _tile.Row);
                }
                else
                {
                    _tile.GameArea.FillTile(_tile.Column, _tile.Row);
                }
            }
        }

    }
}
